package com.ruoyi.job.task;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.omp.api.RemoteSecurityMOService;
import com.ruoyi.omp.api.domain.MaintainOrder;
import com.ruoyi.omp.api.domain.ScheduleTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("scheduleTask")
public class SecurityScheduleTask {
    private static final Logger logger = LoggerFactory.getLogger(SecurityScheduleTask.class);
    @Autowired
    private RemoteSecurityMOService securityMOService;

    /**
     * 生成计划任务
     * me
     */
    public void createScheduleTask(String scheduleId) {
        logger.info("scheduleTask: " + scheduleId);
        MaintainOrder search = new MaintainOrder();
        search.setId(scheduleId);
        //查询任务配置，配置规则
        List<MaintainOrder> maintainOrders = securityMOService.listMO(search).getData();
            //插入 task
        ScheduleTask scheduleTask = new ScheduleTask();
        this.createScheduleTask(maintainOrders.get(0),scheduleTask);
        securityMOService.addScheduleTask(scheduleTask);

    }

    private void createScheduleTask(MaintainOrder maintain,ScheduleTask dest){
        if(dest == null){
            dest = new ScheduleTask();
        }
        dest.setId(IdUtil.getSnowflake(0,0).nextIdStr());
        dest.setTaskName(maintain.getScheduleName());
        dest.setScheduleId(maintain.getId());
        dest.setResponsible(maintain.getResponsible());
        dest.setCreateBy(maintain.getCreateBy());
        dest.setCreateTime(DateUtils.getNowDate());
    }
}
